home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / faxd / FaxMachineLog.c++ < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  108 lines

  1. /*    $Header: /usr/people/sam/fax/faxd/RCS/FaxMachineLog.c++,v 1.17 1994/02/28 14:15:10 sam Rel $ */
  2. /*
  3.  * Copyright (c) 1990, 1991, 1992, 1993, 1994 Sam Leffler
  4.  * Copyright (c) 1991, 1992, 1993, 1994 Silicon Graphics, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and 
  7.  * its documentation for any purpose is hereby granted without fee, provided
  8.  * that (i) the above copyright notices and this permission notice appear in
  9.  * all copies of the software and related documentation, and (ii) the names of
  10.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11.  * publicity relating to the software without the specific, prior written
  12.  * permission of Sam Leffler and Silicon Graphics.
  13.  * 
  14.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  15.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  16.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  17.  * 
  18.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  22.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  23.  * OF THIS SOFTWARE.
  24.  */
  25. #include <ctype.h>
  26. #include <osfcn.h>
  27. #include <syslog.h>
  28. #include <sys/time.h>
  29. #include <sys/stat.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <fcntl.h>
  33.  
  34. #include "config.h"
  35. #include "FaxMachineLog.h"
  36. #include "StackBuffer.h"
  37.  
  38. const fxStr FaxMachineLog::logDir(FAX_LOGDIR);
  39.  
  40. FaxMachineLog::FaxMachineLog(const fxStr& number, mode_t mode)
  41. {
  42.     fxStr canon(number);
  43.     for (int i = canon.length()-1; i >= 0; i--)
  44.     if (!isdigit(canon[i]))
  45.         canon.remove(i,1);
  46.     mode_t omask = umask(022);
  47.     fd = open((char*) (FaxMachineLog::logDir | "/" | canon),
  48.     O_WRONLY|O_APPEND|O_CREAT, mode);
  49.     (void) umask(omask);
  50.     if (fd != -1) {
  51.     pid = getpid();
  52.     log("SESSION BEGIN");
  53.     } else
  54.     syslog(LOG_ERR, "Can not open machine log for \"%s\"", (char*) number);
  55. }
  56.  
  57. FaxMachineLog::~FaxMachineLog()
  58. {
  59.     if (fd != -1) {
  60.     log("SESSION END");
  61.     close(fd);
  62.     }
  63. }
  64.  
  65. void
  66. FaxMachineLog::log(const char* fmt, ...)
  67. {
  68.    if (fd != -1) {
  69.     va_list ap;
  70.     va_start(ap, fmt);
  71.     vlog(fmt, ap);
  72.     va_end(ap);
  73.    }
  74. }
  75.  
  76. void
  77. FaxMachineLog::vlog(const char* fmt0, va_list ap)
  78. {
  79.    if (fd == -1)
  80.     return;
  81.     int oerrno = errno;            // save errno on entry
  82.     char buf[16*1024];
  83.     timeval tv;
  84.     (void) gettimeofday(&tv, 0);
  85.     strftime(buf, sizeof (buf), "%h %d %T", localtime((time_t*) &tv.tv_sec));
  86.     sprintf(buf+strlen(buf), ".%02u: [%5d]: ", tv.tv_usec / 10000, pid);
  87.     /*
  88.      * Copy format string into a local buffer so
  89.      * that we can substitute for %m, a la syslog.
  90.      */
  91.     fxStackBuffer fmt;
  92.     for (const char* fp = fmt0; *fp; fp++) {
  93.     if (fp[0] == '%')
  94.         switch (fp[1]) {
  95.         case '%':
  96.         fmt.put("%%"); fp++;
  97.         continue;
  98.         case 'm':            // substitute errno string
  99.         fmt.put(strerror(oerrno));
  100.         continue;
  101.         }
  102.     fmt.put(fp[0]);
  103.     }
  104.     fmt.put('\n'); fmt.put('\0');
  105.     vsprintf(buf+strlen(buf), (char*) fmt, ap);
  106.     (void) write(fd, buf, strlen(buf));
  107. }
  108.